﻿namespace Z2.Editor
{

	using Z2.Common;
	using System.Runtime.Serialization;
	using System.Xml.Serialization;
	using System.Xml;
	using System;

	public interface IXmlObject
	{
		XmlElement Save(string name, XmlDocument doc);
		void Load(XmlNode element);
	}

	public class EditorFieldObject : EditorField
	{

		#region Constructors

		internal EditorFieldObject()
		{
		}

		public EditorFieldObject(long id, EditorObject obj)
			: base(id, obj)
		{
		}

		public EditorFieldObject(EditorObject obj)
			: base(obj)
		{
			this.Type = ObjectType.Moveable;
			this.Bend = 0;
		}

		#endregion

		#region Properties

		public bool MoveHorizontal
		{
			get;
			set;
		}

		public int Move
		{
			get;
			set;
		}

		public int Bend
		{
			get;
			set;
		}

		public ObjectType Type
		{
			get;
			set;
		}

		#endregion

		public override void Load(XmlNode element)
		{
			base.Load(element);

			this.Type = (ObjectType) Convert.ToInt32(element.Attributes[XmlConst.ATT_TYPE].Value);
			this.Bend = Convert.ToInt32(element.Attributes[XmlConst.ATT_BEND].Value);
			if (Exists(XmlConst.ATT_MOVE, element))
				this.Move = Convert.ToInt32(element.Attributes[XmlConst.ATT_MOVE].Value);
			if (Exists(XmlConst.ATT_MOVE_DIR, element))
				this.MoveHorizontal = Convert.ToBoolean(element.Attributes[XmlConst.ATT_MOVE_DIR].Value);
		}

		public override XmlElement Save(string name, XmlDocument doc)
		{
			XmlElement elm = base.Save(name, doc);
			elm.Attributes.Append(CreateAttribute(XmlConst.ATT_TYPE, Convert.ToInt32(this.Type).ToString(), doc));
			elm.Attributes.Append(CreateAttribute(XmlConst.ATT_BEND, Convert.ToString(this.Bend), doc));
			elm.Attributes.Append(CreateAttribute(XmlConst.ATT_MOVE, Convert.ToString(this.Move), doc));
			elm.Attributes.Append(CreateAttribute(XmlConst.ATT_MOVE_DIR, Convert.ToString(this.MoveHorizontal), doc));

			return elm;
		}

	}
}
